package com.one.common.view.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.one.common.manager.event.BusManager;
import com.one.common.presenter.BasePresenter;
import com.one.common.utils.Toaster;
import com.one.common.view.pagestate.page.PageInterFace;
import com.trello.rxlifecycle2.components.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Author：zhulunjun
 * Time：2017/9/13
 * description：fragment基类
 */

public abstract class BaseFragment<T extends BasePresenter> extends RxFragment implements PageInterFace {
    public T mPresenter;
    public BaseActivity mActivity;
    public Context mContext;
    private Unbinder unbinder;
    public View mView;
    public ViewGroup mParentsView;
    public LayoutInflater mInflater;
    public String TAG;
    public ProgressDialog loadingDialog; // 网络加载弹窗

    public Context getContext() {
        return mContext;
    }

    //获取presenter

    /**
     * 获取IPresenter
     * 如果没有初始化，则用父类的对象
     */
    protected BasePresenter getPresenter() {
        if (mPresenter == null) {
            return new BasePresenter(this, getActivity());
        } else {
            return mPresenter;
        }
    }

    //初始化presenter
    protected abstract void initPresenter();


    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void beforeCreateView() {

    }

    @Override
    public void afterCreateView() {
        unbinder = ButterKnife.bind(this, mView);
        ARouter.getInstance().inject(this);
        initPresenter();
        initRxBus();

    }

    @Override
    public int getSecondLayoutResId() {
        return -1;
    }

    @Override
    public boolean haveRootView() {
        return false;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
        this.mActivity = (BaseActivity) activity;
        TAG = getClass().toString();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        mInflater = inflater;
        mParentsView = container;
        beforeCreateView();
        if (getLayoutResId() != -1 && !haveRootView()) {
            mView = inflater.inflate(getLayoutResId(), container, false);
        } else {
            mView = inflater.inflate(getSecondLayoutResId(), container, false);
        }
        afterCreateView();
        getPresenter().onCreateView(inflater, container, savedInstanceState);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getPresenter().onViewCreated(view, savedInstanceState);
        initView();
        initData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getPresenter().onActivityCreated(savedInstanceState);
        initLoading();
    }

    /**
     * 初始化loading
     */
    private void initLoading() {
        if (loadingDialog == null) {
            loadingDialog = new ProgressDialog(getActivity());
        }
    }

    @Override
    public void showLoading() {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    @Override
    public void cancelLoading() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        getPresenter().onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        getPresenter().onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        getPresenter().onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        getPresenter().onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getPresenter().onDestroy();
        unbinder.unbind();
        BusManager.getBus().unregister(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }

    /**
     * toast提示
     */
    public void showToast(String msg) {
        Toaster.showShortToast(msg);
    }


    @Override
    public void finishPage() {
        mActivity.finish();
    }

    private void initRxBus() {
        BusManager.getBus().register(this);
    }

}
